package com.biyabi.service.impl;

import com.biyabi.dao.AddressMapper;
import com.biyabi.entity.Address;
import com.biyabi.entity.AddressExample;
import com.biyabi.service.AddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressMapper addressMapper;
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<com.biyabi.entity.Address> findAllByUser(String userId){
        AddressExample example = new AddressExample();
        example.createCriteria().andUseridEqualTo(userId);
        return addressMapper.selectByExample(example);
    }
    @Override
    public void add(Address address){
        address.setId(UUID.randomUUID().toString());
        addressMapper.insert(address);
    }
    @Override
    public void update(Address address){
        addressMapper.updateByPrimaryKey(address);
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Address findOne(String id){
        return addressMapper.selectByPrimaryKey(id);
    }
    @Override
    public void delete(String id){
        addressMapper.deleteByPrimaryKey(id);
    }
}
